<?php
/***
 * 产品模板分类
 *
 *
 * @author    lanhongjie <lanhj@yunys.com.cn>
 * @copyright 2016 QuanZhouYunYiShang Inc.
 */

namespace App\Services\ProdTempCategoryService;
use App\Services\ImgServices\ImgTool;
use Illuminate\Support\Facades\Log;
use Validator;
use Illuminate\Support\Facades\DB;
use Illuminate\Http\Request;
class ProdTempCategoryBackService
{
    protected $imgTool;
    /**
     * ProdTempCategoryBackService constructor.
     */
    public function __construct(ImgTool $imgTool)
    {
        $this->imgTool=$imgTool;
    }

    public function addValidator(array $productCategory)
    {
        $v = [
            'name' => 'required|max:10',
            'description' => 'max:200',
            'original_image'=>'required|image',
        ];
        return  Validator::make(
            $productCategory,$v

        );
    }

    /***
     * 分类更新校验
     * @param array $productCategory
     * @return mixed
     */
    public function updateValidator(array $productCategory)
    {
        $v = [
            'name' => 'required|max:10',
            'description' => 'max:200',
            'original_image'=>'|image',
        ];
        return  Validator::make(
            $productCategory,$v

        );
    }
    /**
     * 通过名称获取非删类型
     * @param $name
     */
    public function getProductTempForNameIsNotCurrent($id,$name){
        $product_temp_category=DB::table('product_temp_categorys')->where(array('name'=>$name,'state'=>1))
                ->where('id','<>',$id)->first();
        return $product_temp_category;
    }

    /**
     * 添加产品类型
     * @param array $productClassifys
     * @return mixed
     */
    public function addProductTempCategory(array $productCategory){
        try{
        DB::beginTransaction();
        $productCategory['created_at']=time();
        $productCategory['updated_at']=time();
        $productCategory['state']=1;
        $original_image=$productCategory['original_image'];
        unset($productCategory['original_image']);
        if(!empty($original_image)){
            $original_image_url=$this->imgTool->imgSave($original_image,2);
            if(!empty($original_image_url)){
                $productCategory['original_image']=$original_image_url;
            }
        }
        $num = DB::table('product_temp_categorys')->insert($productCategory);
        return ($num==1)?true:false;
        }catch (\Exception $e){
            DB::rollback();
            Log::info($e);
            return false;
        }finally{
            DB::commit();
        }
    }

    /**
     * 修改类型
     * @param $productClassifys
     * @return mixed
     */
    public function updateProductTempCategory($product_temp_category){
        $product_temp_category['updated_at'] = time();
        if(isset($product_temp_category['original_image'])){
            $original_image=$product_temp_category['original_image'];
            unset($product_temp_category['original_image']);
            if(!empty($original_image)){
                $original_image_url=$this->imgTool->imgSave($original_image,2);
                if(!empty($original_image_url)){
                    $product_temp_category['original_image']=$original_image_url;
                }
            }
        }
        $num =  DB::table("product_temp_categorys")->where('id', $product_temp_category['id'])->update($product_temp_category);
        return $num;
    }

    /***
     * 判斷产品分类是不是存在
     * @param $category_id
     * @return bool
     */
    public function prodCategoryExistById($category_id){
        if(!is_int($category_id)){
            return false;
        }
        $num =  DB::table("product_temp_categorys")->where(array('id'=>$category_id,'state'=>1))->count();
        return ($num==1)?true:false;
    }
    /**
     * 获取类型列表
     * @param $request
     */
    public function getProdTempCategoryList($request)
    {
        $search = $request->input('search','');
        $product_temp_categorys = DB::table('product_temp_categorys')->where('state',1)
            ->where('name','like','%'.$search.'%')
            ->orderBy('type')
            ->orderBy('updated_at','DESC')
            ->paginate(10);
        $product_temp_categorys->appends(['search' => $search])->render();
        $product_temp_categorys->search = $search;
        return $product_temp_categorys;
    }

    /***
     * 搜索产品模板类型
     * @param $request
     * @return mixed
     */
    public function searchProdTempCategoryList($request)
    {
        $search = $request->input('search','');
        $product_temp_categorys= DB::table('product_temp_categorys')->where('state',1)
            ->where('name','like','%'.$search.'%')
            ->where(array('type'=>2))
            ->select('name','id','keywords','parent_id','description')
            ->get();
        return $product_temp_categorys;
    }

    /***
     * 获取所有产品模板分类
     * @param bool $show_special_category true 显示所有分类，包括“全部”的分类
     * @return mixed
     */
    public function getAllProdTempCategoryList($show_special_category=false)
    {
        $query= DB::table('product_temp_categorys')
                ->where('state',1);
        if(!$show_special_category){
            $query->where('type',2);
        }
        $query ->select('name','id as category_id');
        $product_temp_categorys =$query ->get()
            ->toArray();
        return $product_temp_categorys;
    }
    /**
     * 根据ID获取未删除类型
     * @param $brand_id
     * @return mixed
     */
    public function getProductTempCategoryForId($product_classifys_id)
    {
        return DB::table("product_temp_categorys")->where(array('state'=>1,'id'=>$product_classifys_id))->first();
    }

    /***
     * 删除产品分类
     * @param $product_classifys_id
     * @return mixed
     */
    public function deleteProductCtegorys($product_classifys_id)
    {
        $productClassifys['state'] = 0;
        $productClassifys['updated_at'] = time();
        $num =  DB::table("product_temp_categorys")->where('id', $product_classifys_id)->update($productClassifys);
        return $num;
    }
}